package karaoke.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;

import karaoke.excepcion.DAOExcepcion;
import karaoke.modelo.Cliente;
import karaoke.util.ConexionBD;

public class ClienteDAO extends BaseDAO {

	public Cliente insertar(Cliente vo) throws DAOExcepcion {
		String query = "insert into cliente(nombres,apellidos,dni,email,telefono,clave) values (?,?,?,?,?,?)";
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			stmt.setString(1, vo.getNombres());
			stmt.setString(2, vo.getApellidos());
			stmt.setInt(3, vo.getDni());
			stmt.setString(4, vo.getEmail());
			stmt.setInt(5, vo.getTelefono());
			stmt.setString(6, vo.getClave());
						
			int i = stmt.executeUpdate();
			if (i != 1) {
				throw new SQLException("No se pudo insertar");
			}
			// Obtener el ultimo id
			int id = 0;
			query = "select last_insert_id()";
			stmt = con.prepareStatement(query);
			rs = stmt.executeQuery();
			if (rs.next()) {
				id = rs.getInt(1);
			}
			vo.setIdCliente(id);

		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return vo;
	}


	public Cliente obtener(int idcliente) throws DAOExcepcion {
	Cliente vo = new Cliente();
	Connection con = null;
	PreparedStatement stmt = null;
	ResultSet rs = null;
	try {
		String query = "select IdCliente, nombres, apellidos, dni, email, telefono, clave from Cliente where IdCliente=?";
		con = ConexionBD.obtenerConexion();
		stmt = con.prepareStatement(query);
		stmt.setInt(1, idcliente);
		rs = stmt.executeQuery();
		if (rs.next()) {
			vo.setIdCliente(rs.getInt(1));
			vo.setNombres(rs.getString(2));
			vo.setApellidos(rs.getString(3));
			vo.setDni(rs.getInt(4));
			vo.setEmail(rs.getString(5));
			vo.setTelefono(rs.getInt(6));
			vo.setClave(rs.getString(7));
			
		}
	} catch (SQLException e) {
		System.err.println(e.getMessage());
		throw new DAOExcepcion(e.getMessage());
	} finally {
		this.cerrarResultSet(rs);
		this.cerrarStatement(stmt);
		this.cerrarConexion(con);
	}
	return vo;
	}
	
	public Cliente obtenerdni(int dni) throws DAOExcepcion {
	Cliente vo = null;
	Connection con = null;
	PreparedStatement stmt = null;
	ResultSet rs = null;
	try {
		String query = "select IdCliente, nombres, apellidos, dni, email, telefono, clave from Cliente where dni=?";
		con = ConexionBD.obtenerConexion();
		stmt = con.prepareStatement(query);
		stmt.setInt(1, dni);
		rs = stmt.executeQuery();
		if (rs.next()) {
			vo= new Cliente();
			vo.setIdCliente(rs.getInt(1));
			vo.setNombres(rs.getString(2));
			vo.setApellidos(rs.getString(3));
			vo.setDni(rs.getInt(4));
			vo.setEmail(rs.getString(5));
			vo.setTelefono(rs.getInt(6));
			vo.setClave(rs.getString(7));
			
		}
	} catch (SQLException e) {
		System.err.println(e.getMessage());
		throw new DAOExcepcion(e.getMessage());
	} finally {
		this.cerrarResultSet(rs);
		this.cerrarStatement(stmt);
		this.cerrarConexion(con);
	}
	return vo;
	}
	
	public Cliente obtenerm(String email) throws DAOExcepcion {
	Cliente vo = null;
	Connection con = null;
	PreparedStatement stmt = null;
	ResultSet rs = null;
	try {
		String query = "select IdCliente, nombres, apellidos, dni, email, telefono, clave from Cliente where email=?";
		con = ConexionBD.obtenerConexion();
		stmt = con.prepareStatement(query);
		stmt.setString(1, email);
		rs = stmt.executeQuery();
		if (rs.next()) {
			vo= new Cliente();
			vo.setIdCliente(rs.getInt(1));
			vo.setNombres(rs.getString(2));
			vo.setApellidos(rs.getString(3));
			vo.setDni(rs.getInt(4));
			vo.setEmail(rs.getString(5));
			vo.setTelefono(rs.getInt(6));
			vo.setClave(rs.getString(7));
			
		}
	} catch (SQLException e) {
		System.err.println(e.getMessage());
		throw new DAOExcepcion(e.getMessage());
	} finally {
		this.cerrarResultSet(rs);
		this.cerrarStatement(stmt);
		this.cerrarConexion(con);
	}
	return vo;
	}
	
	public Cliente validar(Cliente vo)
			throws SQLException {
		
		try {
			Connection con = ConexionBD.obtenerConexion();

			String query = "select idcliente, nombres, apellidos, dni, email,telefono,clave from cliente where email=? and clave=?";
			
			PreparedStatement stmt = con.prepareStatement(query);
			stmt.setString(1, vo.getEmail());
			stmt.setString(2, vo.getClave());
			ResultSet rs = stmt.executeQuery();

			if (rs.next()) {
				vo.setIdCliente(rs.getInt("idCliente"));
				vo.setNombres(rs.getString("nombres"));
				vo.setApellidos(rs.getString("apellidos"));
				vo.setDni(rs.getInt("dni"));
				vo.setEmail(rs.getString("email"));
				vo.setTelefono(rs.getInt("telefono"));
				vo.setClave(rs.getString("clave"));
				System.out.println("Bienvenido " + vo.getNombres() + vo.getApellidos()) ;

				
			} else {
				throw new SQLException("No existe. Usuario o password incorrecto  "+ vo.getEmail());
			}

			rs.close();
			stmt.close();
			con.close();
		} catch (SQLException e) {
			System.out.println(e.getMessage());
			throw new SQLException(e.getMessage());
		}
		return vo;
	}
	public void eliminar(int IdCliente) throws DAOExcepcion {
		String query = "delete from Cliente WHERE IdCliente=?";
		Connection con = null;
		PreparedStatement stmt = null;
		try {
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			stmt.setInt(1, IdCliente);
			int i = stmt.executeUpdate();
			if (i != 1) {
				throw new SQLException("No se pudo eliminar");
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
	}

	
	public Cliente actualizar(Cliente vo) throws DAOExcepcion {
		String query = "update Cliente set nombres=?,apellidos=?,dni=?,email=?,telefono=?,clave=? where IdCliente=?";
		Connection con = null;
		PreparedStatement stmt = null;
		try {
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			stmt.setString(1, vo.getNombres());
			stmt.setString(2, vo.getApellidos());
			stmt.setInt(3, vo.getDni());
			stmt.setString(4, vo.getEmail());
			stmt.setInt(5, vo.getTelefono());
			stmt.setString(6, vo.getClave());
			
			int i = stmt.executeUpdate();
			if (i != 1) {
				throw new SQLException("No se pudo actualizar");
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return vo;
	}

	
	public Collection<Cliente> listar() throws DAOExcepcion {
		Collection<Cliente> c = new ArrayList<Cliente>();
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			con = ConexionBD.obtenerConexion();
			String query = "select IdCliente,nombres,apellidos,dni,email,telefono,clave from Cliente order by nombres";
			stmt = con.prepareStatement(query);
			rs = stmt.executeQuery();
			while (rs.next()) {
				Cliente vo = new Cliente();
				vo.setIdCliente(rs.getInt("IdCliente"));
				vo.setNombres(rs.getString("nombres"));
				vo.setApellidos(rs.getString("apellidos"));
				vo.setDni(rs.getInt("dni"));
				vo.setEmail(rs.getString("email"));
				vo.setTelefono(rs.getInt("telefono"));
				vo.setClave(rs.getString("clave"));
				c.add(vo);
			}

		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return c;
	}


}

